<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/7/30
 * Time: 11:00
 */
namespace app\api\controller\supplier;

use app\api\controller\BasicUserApi;
use app\api\service\MemberService;
use think\Db;
use think\Exception;

/**
 * 供应商入驻控制器
 * Class Index
 * @package app\api\controller\supplier
 */
class Index extends BasicUserApi
{
    /**
     * 确认入住
     */
    public function admission()
    {
        try{
            $post=$this->request
                ->only(['name','tel','supplier_type_ids','intro','images','main','grade_id',
                'phone','province','city','district','headimg','address','lng','lat',
                'code'],'post');
            $validate=new \app\api\validate\SupplierInfo();
            if(!$validate->check($post)){
                return json(['msg'=>$validate->getError()],403);
            }
            //判断该用户是否已经入驻过了
            if(get_login_info($this->request->param('token'),'type')>0){
                //已经入驻了
                return json(['msg'=>'您已经入驻过了'],403);
            }
            $post['expiry_time']=time();
            $post['mid']=UID;
            $pay_log_id=0;
            Db::transaction(function () use ($post,&$pay_log_id) {
                //删除供应商信息
                Db::name('supplier_info')->where('mid',UID)->delete();
                //添加供应商信息
                Db::name('supplier_info')->strict(false)->insert($post);
                //更新会员信息
                $sm_data['phone']=$post['phone'];
                $sm_data['province']=$post['province'];
                $sm_data['city']=$post['city'];
                $sm_data['district']=$post['district'];
                if($post['headimg']){
                    $sm_data['headimg']=$post['headimg'];
                }
                $sm_data['address']=$post['address'];
                $sm_data['lng']=$post['lng'];
                $sm_data['lat']=$post['lat'];
                $sm_data['type']=USER_TYPE_SUPPLIER;
                Db::name('store_member')->where('id',$post['mid'])->update($sm_data);
                if(MemberService::is_full_info(UID)){
                    log_credit_change(UID,PERFECT_INFORMATION_CREDIT,PERFECT_INFORMATION,'完善资料加信用积分');
                }
                //查询入驻等级
                $store_member_grade=Db::name('store_member_grade')
                    ->where('id',$post['grade_id'])->field('month,money')->find();
                //插入入驻信息
                $ma_data['mid']=$post['mid'];
                $ma_data['type']=USER_TYPE_SUPPLIER;
                $ma_data['month']=$store_member_grade['month'];
                $ma_data['money']=$store_member_grade['money'];
                $ma_data['create_at']=time();
                $ma_data['pay_time']=0;
                $ma_data['grade_id']=$post['grade_id'];
                $ma_id=Db::name('member_admission')->insertGetId($ma_data);

                //插入支付信息
                $pl_data['order_sn']=create_order_sn(16,'admission');
                $pl_data['order_id']=$ma_id;
                $pl_data['money']=$ma_data['money'];
                $pl_data['type']=ORDER_TYPE_ADMISSION;
                $pl_data['pay_type']=PAY_TYPE_WEXIN;
                $pl_data['pay_status']=0;
                $pay_log_id=Db::name('pay_log')->insertGetId($pl_data);
            });
            //跳转到支付
            return action('api/other.pay/weixin',['pay_log_id'=>$pay_log_id,'code'=>$post['code']]);

        }catch(\Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

    /**
     * 修改供应商信息
     * @return \think\response\Json
     */
    public function update_info(){
        try{
            $post=$this->request
                ->only(['name','tel','supplier_type_ids','intro','images','main','phone',
                    'province','city','district','headimg','address','lng','lat'],'post');
            $validate=new \app\api\validate\SupplierInfo();
            if(!$validate->scene('edit')->check($post)){
                return json(['msg'=>$validate->getError()],403);
            }
            //更新会员信息
            $sm_data['phone']=$post['phone'];
            $sm_data['province']=$post['province'];
            $sm_data['city']=$post['city'];
            $sm_data['district']=$post['district'];
            if($post['headimg']){
                $sm_data['headimg']=$post['headimg'];
            }
            $sm_data['address']=$post['address'];
            $sm_data['lng']=$post['lng'];
            $sm_data['lat']=$post['lat'];
            Db::name('store_member')->where('id',UID)->update($sm_data);
            //更新供应商信息
            Db::name('supplier_info')->strict(false)->where('mid',UID)->update($post);
            return json(['msg'=>'更新成功'],200);
        }catch (Exception $e){
            return json(['msg'=>$e->getMessage()],500);
        }
    }

}
